package com.jowell.mall.product.mq;

import com.jowell.mall.product.config.MQConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Mr.ZJW
 * @Date: 2022-07-12 9:30
 * @Description:
 */
@RestController
@RequestMapping("/mq")
public class MQProduct {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String MQSend(String msg) {
        rabbitTemplate.convertAndSend(MQConfig.DIRECT_EXCHANGE, MQConfig.DIRECT_QUERY, msg, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //消息持久化
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            }
        });
        return "发送成功";
    }

}
